<template>
  <BasicModal v-bind="$attrs" @register="register" :title="getTitle" @ok="submitAudit">
    <a-form ref="formRef" :model="fdata">
      <a-textarea v-model:value="fdata.Cause" rows="3" cols="16" />
    </a-form>
  </BasicModal>
</template>

<script lang="ts" setup>
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { ref, reactive } from 'vue';
  import { auditBack, auditPass } from '../LabEntrust.api';
  import { useUserStore } from '/@/store/modules/user';
  import { defHttp } from '/@/utils/http/axios';

  const userStore = useUserStore();
  const emit = defineEmits(['ok1']);
  const getTitle = ref('');
  const fdata = reactive({
    Cause: '',
    oper: '',
  });
  const formData = reactive({
    returnCause: '',
    returnPerson: '',
    firstCause: '',
    firstAudit: '',
    firstAuditId: '',
    auditMark: '',
    operateMark: '',
  });

  //根据点击“不同意”、“同意”按钮显示不同的内容
  const [register, { setModalProps }] = useModalInner((data) => {
    setModalProps({
      minHeight: 40,
    });
    Object.assign(formData, data.formData);
    fdata.oper = data.str;
    if (fdata.oper == 'yes') {
      fdata.Cause = '同意';
      getTitle.value = '初审意见';
    } else {
      fdata.Cause = '';
      getTitle.value = '退回原因';
    }
  });

  //填写意见后，点确认按钮调用的方法
  async function submitAudit() {
    if (fdata.oper == 'yes') {
      //初审通过调用方法
      formData.auditMark = '初审通过';
      formData.operateMark = '委托初审通过';
      formData.firstCause = fdata.Cause;
      formData.firstAudit = userStore.getUserInfo.realname;
      formData.firstAuditId = userStore.getUserInfo.id;
      formData.returnCause = '';
      formData.returnPerson = '';
      console.log('formData', formData);
      // 初审同意更新签名
      // updateSign();
      await auditPass(formData);
      emit('ok1');
    } else {
      //初审退回调用方法
      formData.auditMark = '初审退回';
      formData.operateMark = '委托初审退回';
      formData.returnCause = fdata.Cause;
      formData.returnPerson = userStore.getUserInfo.realname;
      formData.firstCause = '';
      formData.firstAudit = '';
      formData.firstAuditId = '';
      await auditBack(formData);
      emit('ok1');
    }
  }
  // 更新签名
  function updateSign() {
    let signature = '/entrust/labEntrust/signature';
    let servestoken = '/entrust/labEntrust/serveToken';
    //获取服务token和员工票据,员工id
    defHttp.get({ url: servestoken }, { isTransformResponse: false }).then((srcs) => {
      //员工id
      let yaunid = srcs.cardid;
      //let yaunid = 43;
      //服务token
      let tokenServe = srcs.tokenServe;
      //let tokenServe = "eyJ0eXAiOiJqd3QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI1MDcyNzUxNDc0NDM0Mzc1NjgiLCJhdWQiOiJiYXNlIiwiaXNzIjoiemh1bnplIiwiZXhwIjoxNzAxMzQwNzgzLCJpYXQiOjE3MDEzMzI5ODN9.f4zHoRe2Z0lvE5HTmRQUqOmlYP9ygQhC_X8icNHhsuY";
      //员工票据
      let employeeTicket = srcs.employeeTicket;
      //let employeeTicket = "22a64aa46829dacaca9f4e10221de1b215be653d";
      //封装请求
      let datae = {
        businessCreateUserId: yaunid,
        businessCreateUserPositionId: '',
        content: 'flowlame=12',
        description: '',
        name: '12',
      };
      //调用签证
      //获取签名
      window.SCZZ_SIGN.open({
        token: employeeTicket, //员工票据，通过获取员工票据接口获取得到
        data: datae, //签证数据,用于签证校验，其内容见下文
        type: 'PERSONAL_SIGNATURE', //类型（个人签名:PERSONAL_SIGNATURE，单位签章:CORP_SIGNATURE），根据不同的类型打开对应的签证页面
        mobile: false, //移动端调用时需要传递
        call: async function (res) {
          console.log('res', res);
          if (res.code === 1000) {
            if (res.data.imageUrl) {
              await auditPass(formData);
              emit('ok1');
              // 把签名存在数据库
              let urls = signature + '?id=' + yaunid + '&paramasa=' + res.data.imageUrl;
              defHttp.post({ url: urls }, { isTransformResponse: false });
            }
          }

          //回调，接收一个结果对象，该对象里包含sdk的响应状态码code，响应信息msg，当响应code为1000时有响应数据data，响应数据内容见下文
        },
      });
    });
  }
</script>

<style scoped></style>
